import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class IntroPanel extends JPanel
{ 
	ArrayList _playerClasses = new ArrayList();
	int _white = -1;
	int _black = -1;
	Listener _listener;
	int _xHalf;
	int _dy;
	int _numGames;
	JFrame _frame;
	
	public IntroPanel(JFrame frame, int numGames)
	{
		_frame = frame;
		_numGames = numGames;

		_listener = new Listener();
		addMouseListener(_listener);
	}
	
	public void registerPlayer(Class c)
	{
//		assert (IPlayer.class.isAssignableFrom(c))
//			: "class " + c.getName() + " does not appear to implement IPlayer.";
		_playerClasses.add(c);
	}
	
	public void paint(Graphics g)
	{
		Rectangle bounds = getBounds();
		_dy = Math.min(50, bounds.height / (_playerClasses.size() + 2));
		_xHalf = Math.min(250, bounds.width / 2);
		
		g.setColor(new Color(0x66, 0x99, 0x33));
		g.fillRect(0, 0, bounds.width, bounds.height);
		
		Font normal = new Font("Centrury", 0, 14);
		Font bold = new Font("Centrury", Font.BOLD, 14);

		g.setFont(bold);
		g.setColor(Color.WHITE);
		g.drawString("WHITE", 10, _dy - 2);
		g.drawLine(10, _dy, 100, _dy);

		g.setColor(Color.BLACK);
		g.drawString("BLACK", _xHalf + 10, _dy - 2);
		g.drawLine(_xHalf + 10, _dy, _xHalf + 100, _dy);
		
		for (int i = 0; i < _playerClasses.size(); i++)
		{
			Class c = (Class)_playerClasses.get(i);
			if (i == _white)
			{
				g.setColor(Color.WHITE);
				g.setFont(bold);
			}
			else
			{
				g.setColor(Color.BLUE);
				g.setFont(normal);
			}
			g.drawString(c.getName(), 10, _dy + _dy * (i+1));

			if (i == _black)
			{
				g.setColor(Color.BLACK);
				g.setFont(bold);
			}
			else
			{
				g.setColor(Color.BLUE);
				g.setFont(normal);
			}
			g.drawString(c.getName(), _xHalf + 10, _dy + _dy * (i+1));
		}
	}
	
	private void launchTournament()
	{
		GamePanel panel = new GamePanel();
		panel.setBounds(getBounds());
		hide();
		_frame.getContentPane().remove(this);
		_frame.getContentPane().add(panel);
		panel.show();
		panel.doLayout();
		_frame.doLayout();
		
		IBoard board = new GameBoard(8, 8);
		IGui.get().setBoard(board);

		Class cWhite = (Class)_playerClasses.get(_white);
		Class cBlack = (Class)_playerClasses.get(_black);
		
		IPlayer pWhite, pBlack;
		
		try
		{
			pWhite = (IPlayer)cWhite.newInstance();
			pBlack = (IPlayer)cBlack.newInstance();
		}
		catch (Exception e)
		{
//			assert (false) : e.getMessage();
			_frame.hide();
			return;
		}
		
		board.addPlayer(pWhite, Color.WHITE);
		board.addPlayer(pBlack, Color.DARK_GRAY);
		
		board.beginTournament(_numGames);
		
		// In case we ever get back to this screen.
		_white = _black = -1;
	}


	private class Listener extends MouseAdapter
	{
		public void mouseClicked(MouseEvent e)
		{
			boolean left = e.getPoint().x < _xHalf;
			int y = ((e.getPoint().y - _dy/2) / _dy) - 1;
			if (y >= 0 && y < _playerClasses.size())
			{
				if (left)
					_white = y;
				else
					_black = y;
				repaint();
			}
			if (_white >= 0 && _black >= 0)
				launchTournament();
		}
	}
} 
